﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="Base.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.SqlBulkCopy
{
    using System;

    using Dhgms.DataManager.Model.Helper.Database;
    using Dhgms.TplHelper.Model.Info;
    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Base class for an SQL Bulk Copy Task
    /// </summary>
    public abstract class Base : Task.Base<RecordProcessor>
    {
        #region Constants and Fields

        /// <summary>
        /// Destination Database Connection String
        /// </summary>
        protected string DestinationConnectionString;

        /// <summary>
        /// the name of the destination database
        /// </summary>
        protected string DestinationDatabase;

        /// <summary>
        /// the schema the destination table sits in
        /// </summary>
        protected string DestinationSchema;

        /// <summary>
        /// the table the data is going to end up in
        /// </summary>
        protected string DestinationTable;

        /// <summary>
        /// the delete action to be taken on the target table
        /// </summary>
        protected DestinationTableDeleteAction TargetTableDeleteAction;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Base"/> class. 
        /// </summary>
        /// <param name="taskName">
        /// Name of the data being bulk imported
        /// </param>
        /// <param name="destinationConnectionString">
        /// Connection String for the target database
        /// </param>
        /// <param name="destinationDatabase">
        /// Name of the database the table sits in
        /// </param>
        /// <param name="destinationSchema">
        /// the schema the destination table sits in
        /// </param>
        /// <param name="destinationTable">
        /// the table the data is going to end up in
        /// </param>
        /// <param name="targetTableDeleteAction">
        /// the delete action to be taken on the target table
        /// </param>
        protected Base(
            string taskName, 
            string destinationConnectionString,
            string destinationDatabase,
            string destinationSchema, 
            string destinationTable, 
            DestinationTableDeleteAction targetTableDeleteAction)
            : base("SQL Bulk Import " + taskName)
        {
            if (string.IsNullOrWhiteSpace(destinationConnectionString))
            {
                throw new ArgumentNullException("destinationConnectionString");
            }

            if (string.IsNullOrWhiteSpace(destinationDatabase))
            {
                throw new ArgumentNullException("destinationDatabase");
            }

            if (string.IsNullOrWhiteSpace(destinationSchema))
            {
                throw new ArgumentNullException("destinationSchema");
            }

            if (string.IsNullOrWhiteSpace(destinationTable))
            {
                throw new ArgumentNullException("destinationTable");
            }

            this.DestinationConnectionString = destinationConnectionString;
            this.DestinationDatabase = destinationDatabase;
            this.DestinationSchema = destinationSchema;
            this.DestinationTable = destinationTable;
            this.TargetTableDeleteAction = targetTableDeleteAction;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Fetches the data and passes it on to be bulk inserted
        /// </summary>
        protected abstract void OnFetchAndProcessSourceData();

        /// <summary>
        /// The OnProcess event for doing the donkey work
        /// </summary>
        /// <param name="result">
        /// object that contains result information
        /// </param>
        protected override void OnProcess(RecordProcessor result)
        {
            MSSql10 databaseHelper = new MSSql10();
            switch (this.TargetTableDeleteAction)
            {
                case DestinationTableDeleteAction.Truncate:
                    databaseHelper.TruncateTable(
                        this.DestinationConnectionString,
                        this.DestinationDatabase,
                        this.DestinationSchema,
                        this.DestinationTable);
                    break;
                case DestinationTableDeleteAction.WipeAndReseed:
                    databaseHelper.WipeAndReseed(
                        this.DestinationConnectionString,
                        this.DestinationDatabase,
                        this.DestinationSchema,
                        this.DestinationTable);
                    break;
                case DestinationTableDeleteAction.None:
                    break;
            }

            this.OnFetchAndProcessSourceData();
        }

        #endregion
    }
}